import javax.swing.*;

public class CheckEmpty {
    JTextField txtS1, txtS2;

    //tf为待检查的文本框
    public CheckEmpty(JTextField t1, JTextField t2) {
        txtS1 = t1;
        txtS2 = t2;
    }

    //检查文本框中是否已输入数据
    public boolean check(int i) {
        //仅判断是否文本框为空
        if (i == 0) {
            if (txtS1.getText().length() == 0 || txtS2.getText().length() == 0) {
                JOptionPane.showMessageDialog(null, "请先在文本框中输入数据！");
                return false;
            } else
                return true;
            //判断是否为空、负数、数字
        } else {
            String regex = "[^1234567890.]";
            if (txtS1.getText().length() == 0 || txtS2.getText().length() == 0) {
                JOptionPane.showMessageDialog(null, "请先在文本框中输入数据！");
                return false;
                //正则表达式,输入必须为数字
            } else if (txtS1.getText().matches(regex) || txtS2.getText().matches(regex)) {
                JOptionPane.showMessageDialog(null, "必须输入数字，请重新输入数据！");
                return false;
            } else if (Float.parseFloat(txtS1.getText()) < 0 || Float.parseFloat(txtS2.getText()) < 0) {
                JOptionPane.showMessageDialog(null, "不该为负值，请重新输入数据！");
                return false;
            } else
                return true;
        }
    }
}
